package com.fiveflavors.suittar.gui.mainpanel.view;

import java.net.URL;

import com.fiveflavors.suittar.SuittarCommand;
import com.fiveflavors.suittar.gui.utils.Converter;
import com.fiveflavors.suittar.resources.SuittarResourceManager;

import twitter4j.Status;
import twitter4j.UserWithStatus;

public class SuittarProfileTimeLineView extends SuittarTimeLineView {
	
	@Override
	public StringBuffer getHeaderContents(SuittarTimeLineViewBean suittarTimeLineViewBean) {
		
		UserWithStatus user = suittarTimeLineViewBean.getSelectedUser();

		StringBuffer buf = new StringBuffer();

		if (user.getId() != suittarTimeLineViewBean.getMyUser().getId()) {
			URL closeURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_CLOSE);
			URL refreshURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_REFRESH);
			buf.append("<DIV ALIGN=\"RIGHT\">");
			buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_FRIENDS + "&id=" + user.getScreenName() + "&page=" + suittarTimeLineViewBean.getPage() + "\">");
			buf.append(Converter.convertURL2ImgTagHtml(refreshURL));
			buf.append("</A>&nbsp;");
			buf.append("<A HREF=\"http://suittar20/closepane?id=" + user.getId() + "\">");
			buf.append(Converter.convertURL2ImgTagHtml(closeURL));
			buf.append("</A>&nbsp;");
			buf.append("</DIV>");
		}
		else {
			URL refreshURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_REFRESH);
			buf.append("<DIV ALIGN=\"RIGHT\">");
			buf.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_FRIENDS + "&id=" + user.getScreenName() + "&page=" + suittarTimeLineViewBean.getPage() + "\">");
			buf.append(Converter.convertURL2ImgTagHtml(refreshURL));
			buf.append("</A>&nbsp;");
			buf.append("</DIV>");			
		}

		buf.append("<TABLE>");

		buf.append("<TR VALIGN=\"BOTTOM\">");
		buf.append("<TD ROWSPAN=\"2\">");
		buf.append(Converter.convertURL2ImgTagHtml(user.getProfileImageURL()) + "&nbsp;");
		buf.append("</TD>");
		buf.append("<TD>");
		buf.append("<FONT SIZE=\"+3\">" + user.getScreenName() + "</FONT>");
		if (!user.getScreenName().equals(user.getName())) {
			buf.append("<FONT SIZE=\"+1\"> [" + user.getName() + "]</FONT>");			
		}
		buf.append("</TD>");
		buf.append("</TR>");

		buf.append("<TR VALIGN=\"top\">");
		buf.append("<TD>");
		buf.append(" - " + user.getFavouritesCount() + " Favorites, ");
		buf.append("" + user.getFriendsCount() + " Friends, ");
		buf.append("" + user.getFollowersCount() + " Followers, ");
		buf.append("" + user.getStatusesCount() + " Updates");
		if (suittarTimeLineViewBean.isAuthUser()) {
			buf.append("&nbsp;/&nbsp;your profile.");
		} 
		else {
			if (suittarTimeLineViewBean.isAlreadyFriend()) {
				URL userDelURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_USER_DEL);
				buf.append("&nbsp;/&nbsp;your friend ");
				buf.append("<A HREF=\"http://suittar20/remove?id=" + user.getScreenName() + "\">");
				buf.append(Converter.convertURL2ImgTagHtml(userDelURL));
				buf.append("</A>");
			}
			else {
				URL userAddURL = SuittarResourceManager.getResourceUrl(SuittarResourceManager.ICON_USER_ADD);
				buf.append("&nbsp;/&nbsp;not yet your friend ");
				buf.append("<A HREF=\"http://suittar20/add?id=" + user.getScreenName() + "\">");				
				buf.append(Converter.convertURL2ImgTagHtml(userAddURL));
				buf.append("</A>");
			}
		}
		buf.append("</TD>");
		buf.append("</TR>");

		buf.append("</TABLE>");
		buf.append("<BR>");

		buf.append("<TABLE WIDTH=\"100%\" CELLSPACING=\"1\" CELLPADDING=\"1\">");
		buf.append("<TR VALIGN=\"top\">");
		buf.append("<TD WIDTH=\"15%\">");
		buf.append("Bio");
		buf.append("</TD>");
		buf.append("<TD WIDTH=\"85%\">");
		buf.append(user.getDescription());
		buf.append("</TD>");
		buf.append("</TR>");
		buf.append("<TR VALIGN=\"top\">");
		buf.append("<TD WIDTH=\"15%\">");
		buf.append("Location");
		buf.append("</TD>");
		buf.append("<TD WIDTH=\"85%\">");
		buf.append(user.getLocation());
		buf.append("</TD>");
		buf.append("</TR>");
		buf.append("<TR VALIGN=\"top\">");
		buf.append("<TD WIDTH=\"15%\">");
		buf.append("URL");
		buf.append("</TD>");
		buf.append("<TD WIDTH=\"85%\">");
		if (user.getURL() == null) {
			buf.append("&nbsp;");			
		}
		else {
			String hp =  user.getURL().toString();
			if (!hp.startsWith("http://")) {
				hp = "http://" + hp;
			}
			buf.append("<A HREF=\"" + hp + "\">");
			buf.append(hp);
			buf.append("</A>");			
		}
		buf.append("</TD>");
		buf.append("</TR>");
		buf.append("<TR VALIGN=\"top\">");
		buf.append("<TD WIDTH=\"15%\">");
		buf.append("Protected");
		buf.append("</TD>");
		buf.append("<TD WIDTH=\"85%\">");
		buf.append((user.isProtected()) ? "Yes." : "No.");
		buf.append("</TD>");
		buf.append("</TR>");
				
		buf.append("</TABLE>");
		
		return buf;
	}

	@Override
	public StringBuffer getFooterContents(SuittarTimeLineViewBean suittarTimeLineViewBean) {
		return new StringBuffer("");
	}

	@Override
	public StringBuffer getStatusRowContents(Status status,SuittarTimeLineViewBean timeLineViewBean) {
		String replayBGColor = "";
		if (status.getText().indexOf("@" + timeLineViewBean.getMyUser().getScreenName()) > -1) {
			replayBGColor = " BGCOLOR=\"#ffdead\"";
		}
		StringBuffer buf = new StringBuffer();
		buf.append("<TR><TD" + replayBGColor + ">");
		buf.append(Converter.addAnchorTagInText(status.getText()));			
		buf.append("</TD></TR>");
		buf.append("<TR><TD align=\"right\"" + replayBGColor + ">");
		buf.append(Converter.convertUTCString2JSTString(status.getCreatedAt()));
		buf.append("</TD></TR>");
		return buf;
	}
	

}
